//
//  ChooseTableController.m
//  QuickCure1
//
//  Created by JianFeng on 16/11/1.
//  Copyright © 2016年 maoge. All rights reserved.
//
#define SELF Disease
#import "Disease.h"
#import "NetWorkManager.h"
#import "ChooseTableController.h"
#import <MJRefresh.h>

@interface ChooseTableController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate,UISearchResultsUpdating,UIAlertViewDelegate,UISearchControllerDelegate>
@property(strong, nonatomic) UISearchController *searchController;
@property (strong, nonatomic)NSMutableArray  *searchList;//满足搜索条件的数组

//数据——————————————
//病例类型
@property(nonatomic,assign)int ciId;
@property(nonatomic,assign)int page;
@property(nonatomic,strong)NSArray *xiFenArray;
@end
static NSString *cellID = @"ZFBCell";
@implementation ChooseTableController
//接口
-(void)LoadDataWith:(NSInteger)ciId Page:(int)page
{
    self.xiFenArray = nil;
    self.ciId = (int)ciId;
    NSLog(@"%d",self.ciId);
    self.page = page;
}
//数据加载
-(void)loadDataXiFenArray
{
    self.xiFenArray = [[NSArray alloc]init];
    [[NetWorkManager sharedManager] loadDiseaseSubWithCi_id:_ciId page:_page successBlock:^(NSArray *dtoArray) {
        self.xiFenArray = dtoArray;
        [self.tableView reloadData];
    } errorBlock:^(NSString *error) {
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpLeftButtonItem];
    //行高
    self.tableView.rowHeight = 50;
    //去掉分隔线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //可穿透
    self.navigationController.navigationBar.translucent = YES;
    //数据加载
    if (!self.xiFenArray) {
        [self loadDataXiFenArray];
        //搜索导航栏
        [self setSearchControllerView];
    }
    self.tableView.tableFooterView = [[UIView alloc]init];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellID];
    //self.tableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
    [self pullRefresh];
    
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //控制器使用的时候，就是点击了搜索框的时候
    if (self.searchController.active) {
        return self.searchList.count;
    }
    return self.xiFenArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    cell.textLabel.font = MYROUNDFONT(16);
    
    //设置分割线
    UIImageView *separatView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"table_item_bk_with_top_gray_corner"]];
    [cell.contentView addSubview:separatView];
    
    [separatView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.equalTo(cell.contentView);
        make.height.mas_equalTo(5);
    }];

        //搜索控制数据
    if (self.searchController.active)
    {
        Disease *mondel = self.searchList[indexPath.row];
        cell.textLabel.text = mondel.ci3_name;
        return cell;
    }
    //细分
    Disease *mondel = self.xiFenArray[indexPath.row];
    cell.textLabel.text = mondel.ci3_name;
    return cell;
}

//cell的选中
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //获取数据
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSString * titles = cell.textLabel.text;
    if (self.cellTextbloak) {
                //细分病例数据传递
        _cellTextbloak(titles,0);
}
    //发送通知 给疾病详情页面赋值
    [[NSNotificationCenter defaultCenter]postNotificationName:@"DiseaseSubdivision" object:cell.textLabel.text userInfo:nil];
    [self.navigationController popViewControllerAnimated:YES];
}
//搜索导航栏,
- (void)setSearchControllerView{
    //创建搜索导航栏
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchBar.frame = CGRectMake(0, 0, 0, 44);
    self.searchController.searchBar.placeholder = @"疾病细分搜索";
    
    self.searchController.dimsBackgroundDuringPresentation = false;
    //搜索栏表头视图
    [self.searchController.searchBar sizeToFit];
    //背景颜色
    self.searchController.searchBar.backgroundColor = [UIColor orangeColor];
    self.tableView.backgroundColor = kMyColor;
    //代理
    _searchController.delegate = self;        //设置UISearchControllerDelegate协议代理
    self.searchController.searchResultsUpdater = self;
    _searchController.hidesNavigationBarDuringPresentation = YES; //是否隐藏导航栏
    _searchController.dimsBackgroundDuringPresentation = NO;         //是否添加半透明覆盖层
    //子控件属性
    self.searchController.searchBar.searchBarStyle = UIBarStyleBlack;
    self.searchController.searchBar.showsCancelButton = YES;
    self.searchController.searchBar.showsScopeBar = YES;
    //更改按钮的文字
    UIButton *canceLBtn = [_searchController.searchBar valueForKey:@"cancelButton"];
    [canceLBtn setTitle:@"取消" forState:UIControlStateNormal];
    [canceLBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    self.tableView.tableHeaderView = self.searchController.searchBar;

    self.definesPresentationContext = YES;
}

#pragma mark - searchController delegate
//每输入一个字符都会执行一次
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    NSString *searchString = [self.searchController.searchBar text];
    //谓词搜索这里的SELF.name就是你数据源里的model的属性。SELF就是model的类，这里是按照名字搜索。
    NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF.ci3_name CONTAINS %@", searchString];
    if (self.searchList!= nil) {
        [self.searchList removeAllObjects];
    }
    //过滤数据
    self.searchList= [NSMutableArray arrayWithArray:[self.xiFenArray filteredArrayUsingPredicate:preicate]];
    //刷新表格
    [self.tableView reloadData];
}
//释放搜索栏
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.searchController.searchBar removeFromSuperview];
    self.navigationController.navigationBar.translucent = NO;
}
-(void)dealloc{
    NSLog(@"ChooseTableController释放");
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

- (void)setUpLeftButtonItem{
    
    UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [leftBtn setImage:[UIImage imageNamed:@"jiantou"] forState:UIControlStateNormal];
    
    [leftBtn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    
}


#pragma mark
#pragma mark - 下拉刷新
-(void)pullRefresh{
    
    
    
    __weak ChooseTableController *weakSelf = self;
    
    
    
    
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        weakSelf.xiFenArray = [[NSArray alloc]init];
        [[NetWorkManager sharedManager] loadDiseaseSubWithCi_id:_ciId page:_page successBlock:^(NSArray *dtoArray) {
            self.xiFenArray = dtoArray;
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
        } errorBlock:^(NSString *error) {
        }];
    }];
    
    
    MJRefreshNormalHeader *header = (MJRefreshNormalHeader *)self.tableView.mj_header;
    
    [header setTitle:@"我正在刷新.." forState:MJRefreshStateRefreshing];
    [header setTitle:@"松手就可以刷新" forState:MJRefreshStatePulling];
    [header setTitle:@"刷新完毕" forState:MJRefreshStateNoMoreData];
    
    header.lastUpdatedTimeText = ^(NSDate *date){
        return @"";
    };
    //    header.stateLabel.hidden = YES;
    header.lastUpdatedTimeLabel.hidden = YES;
}



/*
 *  如果想任意跳转，就重写goBack方法
 */
- (void)goBack{
    
    [self.navigationController popViewControllerAnimated:YES];
}


@end










